/**
 * Created by tpeng on 2016/4/8.
 */
/**
 * 初始化导航菜单
 * @constructor
 */
function InitTreeMenu_(){
    $.post("/menu/tree/data", function(jdata){
        if(jdata.code == JsonResult.CODE_OK){
            $.each(jdata.data['0'], function(k,v){
                var $tree_ = $('<ul class="easyui-tree"/>');
                var vlist_ = jdata.data[(v.id+'')]; //查找子项menu数据列表
                if(!vlist_){
                    return true; //continue
                }
                var tdata_ = [];
                $.each(vlist_, function(k1,v1){
                    var r_ = _WarpTreeMenu(jdata.data, (v1.id+''), (v1.pid+''));
                    if(r_)
                        tdata_.push(r_);
                });
                if(!tdata_){
                    return true;
                }
                $tree_.tree({data: tdata_, animate:true}).css({padding: '10px'});
                $tree_.tree({onClick: function(node){
                    if($tree_.tree('isLeaf', node.target)){
                        addTab_({index: (node.id), title: node.text, url:node.attributes.url, iconCls:node.iconCls});
                    }
                }});
                $("#menu").accordion('add', {
                    title: v.name,
                    //iconCls: node.iconCls,
                    content: $tree_
                });
            });
            $('#menu').accordion('select', 0);
        }else{
            $.messager.alert('错误提示','请求导航数据错误!','error');
        }
    });
}

/**
 * 组装 easyui-tree 的数据格式
 * @param treeData
 * @param key
 * @param pkey
 * @returns {{id: *, text: *, state: string}}
 * @private
 */
function _WarpTreeMenu(treeData, key, pkey){
    var tdata_ = _FindItemTreeData(treeData, key, pkey);
    if(!tdata_){
        return null;
    }
    var data_ = {id: tdata_.id, text: tdata_.name, state: 'open', attributes: { url: tdata_.url } };
    var children_ = [];
    var pdata_ = treeData[tdata_.id+''];
    $.each((pdata_ ? pdata_ : []), function(k,v){
        var r_ = _WarpTreeMenu(treeData, (v.id+''), (v.pid+''));
        if(r_)
            children_.push(r_);
    });
    if(children_.length != 0){
        data_.state = 'closed';
        data_.children = children_;
    }
    return data_;
}

/**
 * 根据key 获取Tree数据 {object}
 * @param treeData
 * @param key
 * @param pkey
 * @returns {*}
 * @private
 */
function _FindItemTreeData(treeData, key, pkey){
    var r_ = null;
    var pdata_ = treeData[pkey];
    $.each((pdata_ ? pdata_ : []), function(k,v){
        if(v && typeof(v) == 'object' && (v.id+'') == key){
            r_ = v;
            return false;
        }
    });
    return r_;
}

/**
 * 增加一个Tab选项卡
 * @param tabData_  {index, title, iconCls, url}
 * @private
 */
function addTab_(tabData_){
    if ($('#tabs').tabs('exists', tabData_.index)) {
        $('#tabs').tabs('select', tabData_.index); //选中并刷新
        var currTab = $('#tabs').tabs('getSelected');
        var url = $(currTab.panel('options').content).attr('src');
        if (url != undefined && currTab.panel('options').title != 'Home') {
            $('#tabs').tabs('update', {
                tab: currTab,
                options: {
                    content: _createFrame(url)
                }
            })
        }
    } else {
        var content = _createFrame(tabData_.url);
        $('#tabs').tabs('add', {
            index: tabData_.index,
            title: tabData_.title,
            content: content,
            iconCls: tabData_.iconCls,
            closable: true
        });
    }
}

/**
 * 创建iframe  {Tab content}
 * @param url
 * @returns {string}
 * @private
 */
function _createFrame(url) {
    return '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
}